//
//  ImageDetailCollectionViewCell.swift
//  PhotoTestDemo
//
//  Created by 惠 on 2020/9/15.
//  Copyright © 2020 惠. All rights reserved.
//

import UIKit

class ImageDetailCollectionViewCell: UICollectionViewCell {
    let screenW = UIScreen.main.bounds.width
    
    var colorType: ColorType! {
        didSet {
            switch colorType {
            case .defaults:
                checkItemButton.setImage(UIImage(named: "checkItem", in: Resources.getBundle, compatibleWith: nil), for: .selected)
            case .red:
                checkItemButton.setImage(UIImage(named: "redSelectedImage", in: Resources.getBundle, compatibleWith: nil), for: .selected)
            case .none:
                checkItemButton.setImage(UIImage(named: "checkItem", in: Resources.getBundle, compatibleWith: nil), for: .selected)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //状态栏高度
        var statusH: CGFloat = 0
        if #available(iOS 13.0, *) {
            statusH = UIApplication.shared.windows[0].windowScene?.statusBarManager?.statusBarFrame.size.height ?? 20
        } else {
            statusH = UIApplication.shared.statusBarFrame.size.height
        }
        
        addSubview(imageView)
        addSubview(navView)
        navView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: screenW, height: 40+statusH))
        navView.addSubview(backButton)
        backButton.frame = CGRect(origin: CGPoint(x: 0, y: statusH), size: CGSize(width: 50, height: 40))
        navView.addSubview(checkItemButton)
        checkItemButton.frame = CGRect(origin: CGPoint(x: screenW-50, y: statusH), size: CGSize(width: 40, height: 40))
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    //MARK: -懒加载UI
    lazy var imageView: UIImageView = {
        let imageView = UIImageView(frame: self.bounds)
        imageView.backgroundColor = .clear
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    //导航栏
    lazy var navView: UIView = {
        let navView = UIView()
        navView.backgroundColor = .white
        return navView
    }()
    
    lazy var backButton: UIButton = {
        let backButton = UIButton(type: .custom)
        backButton.setImage(UIImage(named: "backButton1", in: Resources.getBundle, compatibleWith: nil), for: .normal)
        return backButton
    }()
    
    lazy var checkItemButton: UIButton = {
        let checkItemButton = UIButton(type: .custom)
        checkItemButton.setImage(UIImage(named: "uncheckItem", in: Resources.getBundle, compatibleWith: nil), for: .normal)
//        checkItemButton.setImage(UIImage(named: "checkItem", in: Resources.getBundle, compatibleWith: nil), for: .selected)
        return checkItemButton
    }()
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let btnPoint = self.convert(point, to: self.backButton)
        let checkPoint = self.convert(point, to: self.checkItemButton)
        if self.backButton.point(inside: btnPoint, with: event) {
            return self.backButton
        } else if self.checkItemButton.point(inside: checkPoint, with: event) {
            return self.checkItemButton
        }
        return super.hitTest(point, with: event)
    }
}
